package com.baichang.taoshunda.controller;

import com.baichang.framework.controller.BaseController;
import com.baichang.framework.controller.IController;
import com.baichang.framework.controller.IPageController;
import com.baichang.framework.controller.WebBaseController;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.ControllerConstants;
import com.baichang.taoshunda.entity.City;
import com.baichang.taoshunda.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/22 0022.
 */

@Controller
@RequestMapping(ControllerConstants.CITY)
public class CityWController extends WebBaseController {

    @Autowired
    private CityService cityService;


    @RequestMapping("/hotcity")
    public String hotCitypage(HttpServletRequest request, HttpServletResponse response) {
        return "/taoshunda/city/hotcity";
    }

    @RequestMapping("/citymanager")
    public String citymanager(HttpServletRequest request, HttpServletResponse response) {
        return "/taoshunda/city/citymanager";
    }

    /**
     * 热门城市
     */
    @RequestMapping("/queryHotCity")
    public void queryHotCity(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse httpServletResponse) throws ServiceException {
                return cityService.queryHotCity(nowPage, pageSize);
            }
        });
    }

    /**
     * 修改热门城市的排名
     */
    @RequestMapping("/updateSorting")
    public void updateSorting(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                int id = getInt(request, "id", 0);
                int sorting = getInt(request, "sorting", 0);
                return cityService.updateSorting(id, sorting);
            }
        });
    }

    /**
     * 删除热门城市
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                int id = getInt(request, "id", 0);
                return cityService.delete(id);
            }
        });
    }

    /**
     * 修改城市名称
     */
    @RequestMapping("/updateCityName")
    public void updateCityName(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                int id = getInt(request, "id", 0);
                String cityName = getString(request, "name", "");
                return cityService.updateCityName(id, cityName);
            }
        });
    }

    /**
     * 查询城市
     */
    @RequestMapping("/query")
    public void query(HttpServletRequest request, HttpServletResponse response) {
        initPage(request, response, new IPageController() {
            @Override
            public Page initPage(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                int parentId = getInt(request, "parentId", 1);
                int isHot = getInt(request, "isHot", -1);
                String name = getString(request, "cityName", "");
                return cityService.queryAllCity(nowPage, pageSize, parentId, isHot, name);
            }
        });
    }

    /**
     * 保存和修改
     *
     * @param request
     * @param response
     */
    @RequestMapping("/saveOrUpdateCity")
    public void addCity(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                int id = getInt(request, "cityId", 0);
                String name = getString(request, "name", "");
                Long parentId = getLong(request, "parentId", 0L);
                Integer deptPath = getInt(request, "deptPath", 0);
                Integer sorting = getInt(request, "sorting", 0);
                return cityService.addCity(id, name, parentId, deptPath, sorting);
            }
        });
    }


    /**
     * 设置热门城市
     */
    @RequestMapping("/addHot")
    public void hotCity(HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
                int id = getInt(request, "id", 0);
                int isHot = getInt(request, "isHot", 1);
                return cityService.addHot(id, isHot);
            }
        });
    }

    /**
     * 根据省id获取市
     *
     * @param request
     * @param response
     */
    @RequestMapping("/queryCityIdInfo")
    public void queryCityIdInfo(final HttpServletRequest request, HttpServletResponse response) {
        init(request, response, new IController() {
            @Override
            public Object initData(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServiceException {
                Integer id = getInt(request, "cityId", 0);
                return cityService.queryCityIdInfo(id);
            }
        });
    }
}
